package com.amazon.shop.filter.webfilter;

import com.amazon.shop.bean.entity.User;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.exception.BusinessException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

/**
 * 创建时间：12:49 2021/3/27
 *
 * @author lzj
 * @since JDK 1.8.0_171
 * 类说明：
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 */
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String uri= request.getRequestURI();
        if(uri.contains("/user/login")){
            return true;
        }
        Object user1 = request.getSession().getAttribute("user");
        System.out.println("===========user======="+user1);
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            throw new BusinessException(ResultEnum.LOGIN_ERROR);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        //System.out.println(">>>MyInterceptor2>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        //System.out.println(">>>MyInterceptor2>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
    }
}
